/*
地图上所有司机的标记集合
*/

angular.module('driver')
	.factory('Marker', function() {


		//构建自定义信息窗体
		function createInfoWindow(title, content) {
			var info = document.createElement("div");
			info.className = "info";
			

			//可以通过下面的方式修改自定义窗体的宽高
			//info.style.width = "400px";

			// 定义顶部标题
			/*
			var top = document.createElement("div");
			var titleD = document.createElement("div");
			var closeX = document.createElement("img");
			top.className = "info-top";
			titleD.innerHTML = title;
			closeX.src = "http://webapi.amap.com/images/close2.gif";
			closeX.onclick = closeInfoWindow;

			top.appendChild(titleD);
			top.appendChild(closeX);

			info.appendChild(top);
*/

			// 定义中部内容
			var middle = document.createElement("div");
			middle.className = "info-middle";
			
			middle.innerHTML = content;
			info.appendChild(middle);
			middle.onclick = closeInfoWindow;
			// 定义底部内容
			var bottom = document.createElement("div");
			bottom.className = "info-bottom";
			bottom.style.position = 'relative';
			bottom.style.top = '0px';
			bottom.style.margin = '0 auto';
			bottom.onclick = closeInfoWindow;
			//var sharp = document.createElement("img");
			//sharp.src = "http://webapi.amap.com/images/sharp.png";
			//bottom.appendChild(sharp);
			info.appendChild(bottom);
			return info;
		}

		//关闭信息窗体
		function closeInfoWindow() {
			window.map.clearInfoWindow();
		}

		var openInfoWindow = function(infoWin) {
			return function() {
				this.openInfoWindow(infoWin);
			};
		};

		var o = {
			content: {},
			count: 0,

		};
		var preMarkers = [];
		var preReqMarker = [];

		o.doReqMarker = function(newReqs, oldReqs, map) {
			preReqMarker.forEach(function(marker) {
				//marker.removeEventListener('click', openInfoWindow(infoWindow2));
				//map.removeOverlay(marker);
				marker.setMap(null);
				//marker.removeEventListener('click');
			});
			preReqMarker.length = 0;
			newReqs.forEach(function(user) {
				var marker = new AMap.Marker({
					position: new AMap.LngLat(user.location.lng, user.location.lat),
					icon: '/0.png'
				});
				marker.setExtData({
					tel: user.tel,
					address: user.address,			
				});
				preReqMarker.push(marker);
				marker.setClickable(true);
				marker.setAnimation('AMAP_ANIMATION_BOUNCE');
				marker.setMap(map);
				AMap.event.addListener(marker, 'click', function(event) {
					//console.log(event);
					
					var extData = event.target.getExtData();
					var infoWindow = new AMap.InfoWindow({
						isCustom: true, //使用自定义窗体
						content: createInfoWindow(
							'',
							'<div class="card" id="weiz">\
							  <div class="item item-avatar siji">\
							  <img src=\"driver.png\">\
									<a href=\"#/requestinfo/' + user._id +'\" target=\"_self\">电话：' + extData.tel +
							'</a>\
							        <p>地点：' + extData.address +
							'</p>\
									</div></div>\
									<div id="spn" class="amap-info-sharp"></div>'
						),
						offset: new AMap.Pixel(16, -45) //-113, -140
					});
					console.log(infoWindow);
					infoWindow.open(map, marker.getPosition());

					//showAlert(user.username, user.status.online ? '服务暂未开通!' : '我不在线');
					//$scope.showAlert(event.target.Ed.title, event.target.Ed.title);
				});
			});
		};

		o.doDriverMarker = function(newUsers, oldUsers, map) {
			preMarkers.forEach(function(marker) {
				//marker.removeEventListener('click', openInfoWindow(infoWindow2));
				//map.removeOverlay(marker);
				marker.setMap(null);
				//marker.removeEventListener('click');
			});
			preMarkers.length = 0;

			newUsers.forEach(function(user) {
				if (!user.location)
					return;
				var marker = new AMap.Marker({
					position: new AMap.LngLat(user.location.lng, user.location.lat),

				});

				marker.setExtData({
					title: user.others.realname,
					face: user.images.face,
					address: user.formatted_address,					
				});

				preMarkers.push(marker);
				marker.setClickable(true);
				marker.setAnimation('AMAP_ANIMATION_DROP');
				marker.setMap(map);
				AMap.event.addListener(marker, 'click', function(event) {					
					var extData = event.target.getExtData();
					//console.log(extData);
					var infoWindow = new AMap.InfoWindow({
						isCustom: true, //使用自定义窗体
						content: createInfoWindow(
							'',
							'<div class="card">\
							  <div class="item item-avatar siji">\
								<img src=\'' +
							extData.face + '\'>\
									<h2>' + extData.title +
							'师傅</h2>\
							        <p>地点：' + extData.address +
							'</p>\
									</div></div>\
									<div id="spn" class="amap-info-sharp"></div>'
						),
						offset: new AMap.Pixel(16, -35) //-113, -140
					});
					console.log(infoWindow);
					infoWindow.open(map, marker.getPosition());

					//showAlert(user.username, user.status.online ? '服务暂未开通!' : '我不在线');
					//$scope.showAlert(event.target.Ed.title, event.target.Ed.title);
				});
			});
		};

		o.addMarkers = function(users, map, showAlert) {
			//console.log(users, map);
			//var newMarkers = [];
			users.forEach(function(user) {
				if (!!o.content[user._id]) {
					o.content[user._id].setMap(null);
					delete o.content[user._id];
					o.count--;
				}
				//console.log(user);
				var marker = new AMap.Marker({

					position: new AMap.LngLat(user.location.lng, user.location.lat),
					title: user.others.realname,
					face: user.images.face,
					address: user.formatted_address,
					//icon: user.images.face,
					/*
					icon: new AMap.Icon({
						size: new AMap.Size(28, 37),
						image: user.images.face,
						imageOffset: new AMap.Pixel(-28, 0),
					})
					*/
				});
				marker.setClickable(true);
				marker.setMap(map);
				AMap.event.addListener(marker, 'click', function(event) {
					var infoWindow = new AMap.InfoWindow({
						isCustom: true, //使用自定义窗体
						content: createInfoWindow(
							'',
							'<div class="card">\
							  <div class="item item-avatar siji">\
								<img src=\'' +
							event.target.Ed.face + '\'>\
									<h2>' + event.target.Ed.title +
							'师傅</h2>\
							        <p>地点：' + event.target.Ed.address +
							'</p>\
									</div></div>'
						),
						offset: new AMap.Pixel(16, -45) //-113, -140
					});
					console.log(infoWindow);
					infoWindow.open(map, marker.getPosition());

					//showAlert(user.username, user.status.online ? '服务暂未开通!' : '我不在线');
					//$scope.showAlert(event.target.Ed.title, event.target.Ed.title);
				});
				o.content[user._id] = marker;
				o.count++;
				//newMarkers.push( marker );
			});

			//return newMarkers;
		};

		o.removeMarker = function(userids) {
			userids.forEach(function(id) {
				if (!!o.content[id]) {
					o.content[id].setMap(null);
					delete o.content[id];
					o.count--;
				}
			});
		};

		return o;
	});
